SHELL  = /bin/bash
MIRROR = "http://mi.mirror.garr.it/mirrors/debian/"
DIST   = "testing"
ARCH   = "amd64"

# Download the list of files in Debian
deb_indexes :
	mkdir -p "${DIST}/${ARCH}"
	wget --directory-prefix "${DIST}/${ARCH}" "${MIRROR}dists/${DIST}/main/Contents-amd64.gz"
	wget --directory-prefix "${DIST}/${ARCH}" "${MIRROR}dists/${DIST}/main/binary-amd64/Packages.gz"
	gunzip "${DIST}/${ARCH}/"*.gz

# Find the list of (binary) packages that contain manpages
find_packages :
	./find_packages.py

# Download all the binaries containing the manpages
download_binaries :
	mkdir -p binaries
	wget --mirror --no-host --no-parent --no-directories \
	     --directory-prefix binaries --execute robots=off \
	     --input-file packages.url

# Find all the manpages within the binaries and extract them
extract_pages :
	for deb in binaries/*.deb ; do
		7z x -so "$${deb}" \
			| tar --extract --strip-components 3 --file - ./usr/share/man/
	done

# Decompress all pages into .roff files
manpages2roff : $(subst :,\:,$(patsubst %.gz,%.roff,$(shell find ./man -type f -name "*.gz")))
%.roff : %.gz
	@gunzip --stdout "$<" > "$@"

# Convert pages to TEXT
# Mandoc doesn't look for include manpages ".so" outside of the current working directory
# or /usr/share/man for security reasons, so we need to cd into ./man in order to set
# it as the working directory.
manpages2txt : $(subst :,\:,$(patsubst %.roff,%.txt,$(shell find ./man -type f -name "*.roff")))
%.txt : %.roff
	@ROFF="$<"
	@TEXT="$@"
	@ROFF="$${ROFF:4}"    # Remove "man/" prefix
	@TEXT="$${TEXT:4}"    # Remove "man/" prefix
	
	@cd ./man
	@mandoc -T utf8 -O width=80 "$$ROFF" | col -b > "$$TEXT"

# Convert pages to HTML
# Mandoc doesn't look for include manpages ".so" outside of the current working directory
# or /usr/share/man for security reasons, so we need to cd into ./man in order to set
# it as the working directory.
manpages2html : $(subst :,\:,$(patsubst %.roff,%.html,$(shell find ./man -type f -name "*.roff")))
%.html : %.roff
	@ROFF="$<"
	@HTML="$@"
	@ROFF="$${ROFF:4}"    # Remove "man/" prefix
	@HTML="$${HTML:4}"    # Remove "man/" prefix
	
	@cd ./man
	@mandoc -T html -O fragment "$$ROFF" > "$$HTML"
	@tidy	--clean yes --vertical-space auto --wrap 0 --indent 0 --hide-comments yes \
		--drop-empty-elements no --doctype html5 --logical-emphasis yes \
		--show-body-only yes --quiet yes --write-back yes "$$HTML"

# make page links to other manpages.
# mandoc has the option "-O man="%N.%S"" to convert .Xr macros to links across manpages.
# Unfortunately this seems to work only for .Xr and not for free text such as ls(1).
# So, we try to create the links ourselves.
manpages2html_postprocessing :
	./html_postprocess.py

# Convert pages to pdf
manpages2pdf : $(subst :,\:,$(patsubst %.roff,%.pdf,$(shell find ./man -type f -name "*.roff")))
%.pdf : %.roff
	# Make PDF
	# There is also roff2pdf but DVI seems to produce a nicer output
	@roff2dvi "$${filename}" > "$${manpage}.dvi"
	dvipdf "$${manpage}.dvi" "$${manpage}.pdf"

# Convert pages to docbook
manpages2docbook : $(subst :,\:,$(patsubst %.roff,%.docbook,$(shell find ./man -type f -name "*.roff")))
%.docbook : %.roff
	@timeout 60s doclifter -I "$$PWD/man" < "$<" > "$@"

# Build RDF graph
manpages2rdf:
	./rdf_dump.py

.ONESHELL:
.SUFFIXES:
.PHONY:
.DELETE_ON_ERROR:
